package com.xonro.common.histrans.sfeg.biz.fund;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.consts.DrpConst;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.sfeg.biz.common.SfegProcessTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class FundCheckBiz extends SfegProcessTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();
        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_AM_ASSET_RECEPTION");//资产验收
        logModel.setOuterTable("tb_zcys");//资产申请
        logModel.setOuterPK(mainData.getString("id"));
        logModel.setOuterExt1(mainData.getString("ystitle"));
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("APPLY_NO", mainData.getString("id"));//申请单号
        mainBO.set("TITLE", mainData.getString("sbname"));//相关申请物品
        if(StringUtils.isNotBlank(mainData.getString("ysDate"))) {
            mainBO.set("NODE_HANDLE_TIME", DateUtil.parse(mainData.getString("ysDate"), "yyyy-MM-dd"));//验收日期
        }
        mainBO.set("REMARK", mainData.getString("memo"));//备注
        if(StringUtils.isNotBlank(mainData.getString("filldate"))) {
            mainBO.set("APPLY_DATE", DateUtil.parse(mainData.getString("filldate"), "yyyy-MM-dd"));//申请日期
        }




        List<RowMap> sonDatas= SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM tb_zcysDetail  WHERE ysid = ?  ",mainData.getString("id"));
        for(RowMap son:sonDatas){
            String uuid = UUIDGener.getUUID();
            BOCreateData subBOCreateData = new BOCreateData();
            boCreateDataList.add(subBOCreateData);
            subBOCreateData.setCreateUser(createUser);
            subBOCreateData.setBoName("BO_XR_AM_ASSET_ACCEPTION_ITEM");
            BO subBO = new BO();
            subBO.setId(uuid);
            subBOCreateData.setBo(subBO);

            subBO.set("OA_NO",son.getString("id"));//oa编号
            subBO.set("ASSET_NAME",son.getString("sbname"));//资产名称
            subBO.set("MODEL",son.getString("xxgg"));//规格型号
            subBO.set("PRICE",son.getString("danjia"));//单价
            subBO.set("QUANTITY",son.getString("sl"));//数量
            subBO.set("UNIT",son.getString("danwei"));//单位
            subBO.set("AMOUNT",son.getString("je"));//总价
            subBO.set("LEVEL1_NO",son.getString("type63"));//资产大类
            subBO.set("LEVEL2_NO",son.getString("type64"));//资产小类
            subBO.set("REMARK",son.getString("memo"));//备注
            subBO.set("FACTORY_NUMBER",son.getString("outFactoryNumber"));//出厂编号
            subBO.set("FACTORY_NAME",son.getString("sccj"));//生产厂家
            subBO.set("ASSET_WEIGHT",son.getString("wpjz"));//物品净重
            subBO.set("WARRANTY_PERIOD",son.getString("bxqx"));//保修期限
            subBO.set("FEATURE",son.getString("jstx"));//技术特性
            subBO.set("ASSET_NO",son.getString("GSID"));//资产编码
            subBO.set("DIMENSION",son.getString("wxcc"));//外形尺寸
            super.addLogModel(historyTransContext, "tb_zcysDetail", son.getString("id"), "BO_XR_AM_ASSET_ACCEPTION_ITEM", uuid, bindId);

        }
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);

        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //印章id
        fileTransData.setFkCode(mainData.getString("id"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_AM_ASSET_RECEPTION");
        fileTransData.setBoName("BO_XR_AM_ASSET_RECEPTION");
        fileTransData.setFieldName("ATTACHMENT");//印张图片
    }

    @Override
    protected String getProcessDefId() {
        return "obj_759b1e11b643413688e6eeefbec90b75";
    }

    @Override
    protected String getActivityDefId() {
        return "obj_8023b1bfacbf47e3b7147cc53b5d9f41";
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        String id = ctx.getMainData().getString("id");
        String title = ctx.getMainData().getString("ystitle");
        return id+title;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        return ctx.getMainData().getString("id");
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_zcys";
    }

    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        return historyTransContext.getMainData().getString("id");
    }

    @Override
    protected HistoryTransContext initOneTransContext() {
        return null;
    }

    @Override
    protected List<RowMap> getMainDatas() {
        return SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM tb_zcys WHERE orcode='1040'");
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }
}
